package com.bootdo.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.UUID;

public class FileUtil {

	public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(filePath + File.separator + fileName);
		out.write(file);
		out.flush();
		out.close();
	}

	public static void deleteFile(File file) {
		if(file.isDirectory()) {
			File zFiles[] = file.listFiles();
			for(File file1 : zFiles) {
				deleteFile(file1);
			}
			file.delete();
		} else {
			file.delete();
		}
	}

	public static String renameToUUID(String fileName) {
		return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
	}
}
